<template>
	<div>
		<!-- 母工单列表 -->
		<v-container row class="text-subtitle-1 d-flex justify-center  align-left">
			<v-row class="mt-2">
				<v-col cols="12" sm="3" md="2">
					<v-text-field label="工单号" v-model="query.workcode" dense outlined>
					</v-text-field>
				</v-col>
				<v-col cols="12" sm="3" md="2">
					<v-text-field label="项目名称" v-model="query.projectname" dense outlined>
					</v-text-field>
				</v-col>
				<v-col cols="12" sm="2" md="2">
					<v-text-field label="设备名称" v-model="query.name" dense outlined>
					</v-text-field>
				</v-col>
				<!-- ml-n16 前面加n是间距 -->
				<!-- class="ml-n16 mx-auto" -->
				<v-col cols="12" sm="2" md="2">
					<v-btn depressed color="primary" @click="searchMasterOrders">查询</v-btn>
				</v-col>
			</v-row>
		</v-container>
		<br />
		<v-card>
			<v-data-table small fixed-header dense :headers="masterOrderHeaderList" :items="masterOrderList"
				item-key="id" class="text-no-wrap" show-select :single-select="true" show-expand single-expand
				loading-text="正在加载请稍等......" :loading="masterOrderLoading" :options.sync="masterOrderOptions"
				:footer-props="vuetifyDefaultPage" :server-items-length="masterOrderTotalCount"
				@item-expanded="expandedMasterOrderItemEvent">
				<template v-slot:top>
					<v-toolbar flat>
						<v-toolbar-title>母工单信息</v-toolbar-title>
						<!-- 一根竖线 -->
						<v-divider class="mx-4" inset vertical></v-divider>
					</v-toolbar>
				</template>
				<!-- 母工单状态修改 -->
				<!-- <template v-slot:item.statusname="props">
					<v-edit-dialog large @save="saveMasterStatus(props.item)" save-text="更新状态" cancel-text="取消">
						<a>
							{{ getMasterOrderStatusName(props.item)}}
						</a>
						<v-icon small class="mr-2" color="red">
							mdi-pencil
						</v-icon>
						<template v-slot:input>
							<div class="mt-4 text-h6">
								变更工单状态
							</div>
							<v-select class="mt-6" v-model="props.item.status" :items="masterOrderStatusList"
								label="选择工单状态"></v-select>
						</template>
					</v-edit-dialog>
				</template> -->
				<!-- 母工单编辑修改 -->
				<template v-slot:item.actions="{ item }">
					<div>
						<v-btn v-if="item.status==120" small tile color="deep-orange"
							@click="openMachineChangeMasterDialog(item)">
							设变工单
						</v-btn>

						<v-btn class="ml-2" small tile color="purple" @click="openBackrepairMasterDialog(item)">
							<v-icon small left>
								mdi-wrench
							</v-icon>
							新增返修单
						</v-btn>
						<!-- 修改按钮 -->
						<v-btn class="ml-2" :disabled="item.status==120" small tile color="success"
							@click="openEditMasterDialog(item)">
							<v-icon small left>
								mdi-pencil
							</v-icon>
							编辑
						</v-btn>
						<v-btn class="ml-2" :disabled="item.status==120" small tile color="red"
							@click="closeOrderClick(item)">
							关闭工单
						</v-btn>
						<!-- 删除按钮 -->
						<v-btn small class="ml-2" tile color="red" @click="deleteMasterItem(item)">
							<v-icon small left>
								mdi-delete
							</v-icon>
							删除
						</v-btn>
					</div>
				</template>
				<!-- 母工单展开 -->
				<template v-slot:expanded-item="{ headers, item }">

					<td :colspan="headers.length">
						
								<v-alert text color="blue-grey" class="text-center">子工单详细信息</v-alert>
								<v-simple-table>
									<template v-slot:default>
										<thead>
											<tr>
												<th class="text-center">操作</th>
												<th class="text-center" v-for="header in childrenHeader"
													:key="header.text">
													{{header.text}}
												</th>
											</tr>
										</thead>
										<tbody>
											<tr v-for="every in childrenOrderList" :key="every.id"
												:class="every.isinpruduction==1?'main':''">
												<td class="text-center">
													<v-btn color="primary" :disabled="every.status==-30" fab small dark
														icon @click="openEditChildrenOrderDialog(every)">
														<v-icon>mdi-pencil</v-icon>
													</v-btn>
													<v-btn color="error" fab small dark icon
														@click="deleteChildrenOrderItem(every)">
														<v-icon>mdi-delete</v-icon>
													</v-btn>

												</td>
												<td class="text-center">{{ every.projectcode}}</td>
												<!-- <td class="text-center">{{ every.projectname}}</td> -->
												<td class="text-center">{{ every.code}}</td>
												<td>
													<a v-if="every.isinpruduction==1" style="color: #67C23A;">是</a>
													<a v-else>否</a>
												</td>
												<td>
													<a v-if="every.isprint==1" style="color: #67C23A;">是</a>
													<a v-else>否</a>
												</td>
												<td class="text-center">{{ every.piccode }}</td>
												<td class="text-center"><a :href="every.picurl" target="_Blank">查看图纸</a>
												</td>
												<td class="text-center">{{ every.name }}</td>
												<td class="text-center">{{ every.specifications }}</td>
												<td class="text-center">{{ every.material }}</td>
												<td class="text-center">{{ every.singlecount }}</td>
												<td class="text-center">{{ every.totalcount }}</td>
												<td class="text-center">{{ every.unit }}</td>
												<td class="text-center">{{ every.surface_treatment }}</td>
												<td class="text-center">{{ every.process }}</td>
												<!-- 	<td class="text-center">
														<a v-if="every.process!=null" @click="lookGYInfo(every)">查看工艺</a>
														<a v-else style="color: rgb(198 214 230);">未维护工艺</a>
													</td> -->
												<td class="text-center">{{ every.outinstoragelinkname }}</td>
												<td class="text-center">{{ every.deliverydate }}</td>
												<td class="text-center">{{ every.confirmdate }}</td>
												<td class="text-center">{{ every.typename }}</td>
												<td class="text-center">{{ every.statusname }}</td>
												<td class="text-center">{{ every.accountingbookcode }}</td>
												<td class="text-center">{{ every.description }}</td>
											</tr>
										</tbody>
									</template>
								</v-simple-table>
					
					</td>



				</template>
			</v-data-table>
		</v-card>
		<!-- 编辑母工单对话框 -->
		<v-dialog v-if="editMasterOrderItem!=null" v-model="editDialogVisable" fullscreen
			transition="dialog-bottom-transition">
			<v-card>
				<v-toolbar dark color="primary">
					<v-btn icon dark @click="editDialogVisable = false">
						<v-icon>mdi-close</v-icon>
					</v-btn>
					<v-toolbar-title>母工单信息变更</v-toolbar-title>
					<v-spacer></v-spacer>
				</v-toolbar>
				<v-row>
					<v-col cols="12">
						<v-alert text color="blue-grey" class="text-center">当前母工单信息</v-alert>
						<v-form ref="form" v-model="validMasterOrderForm">
							<v-row>
								<v-col cols="12" md="3">
									<v-text-field disabled v-model="editMasterOrderItem.workcode" label="工单号">
									</v-text-field>
								</v-col>
								<v-col cols="12" md="3">
									<v-text-field disabled v-model="editMasterOrderItem.projectcode" label="项目号">
									</v-text-field>
								</v-col>
								<!-- <v-col cols="12" md="3">
									<v-select disabled v-model="editMasterOrderItem.status"
										:items="this.masterOrderStatusList" label="工单状态"></v-select>
								</v-col> -->
								<v-col cols="12" md="3">
									<v-text-field disabled v-model="editMasterOrderItem.typename" label="工单类型">
									</v-text-field>
								</v-col>
								<v-col cols="12" md="3">
									<v-text-field v-model="editMasterOrderItem.customername" label="客户名称">
									</v-text-field>
								</v-col>
								<v-col cols="12" md="3">
									<v-text-field v-model="editMasterOrderItem.qty" label="数量">
									</v-text-field>
								</v-col>
								<v-col cols="12" md="3">
									<v-text-field v-model="editMasterOrderItem.unitname" label="单位名称">
									</v-text-field>
								</v-col>

								<v-col cols="12" md="3">
									<v-text-field v-model="editMasterOrderItem.machinecode" label="机台号">
									</v-text-field>
								</v-col>
								<v-col cols="12" md="3">
									<v-text-field v-model="editMasterOrderItem.versioncode" label="版本号">
									</v-text-field>
								</v-col>
								<v-col cols="12" md="3">
									<v-menu v-model="issueddateMenu" :close-on-content-click="false" :nudge-right="40"
										transition="scale-transition" offset-y min-width="auto">
										<template v-slot:activator="{ on, attrs }">
											<v-text-field required v-model="editMasterOrderItem.issueddate" label="下发日期"
												readonly v-bind="attrs" v-on="on"></v-text-field>
										</template>
										<v-date-picker locale="zh-cn" v-model="editMasterOrderItem.issueddate"
											@input="issueddateMenu = false"></v-date-picker>
									</v-menu>
								</v-col>
								<v-col cols="12" md="3">
									<v-menu v-model="assemblydateMenu" :close-on-content-click="false" :nudge-right="40"
										transition="scale-transition" offset-y min-width="auto">
										<template v-slot:activator="{ on, attrs }">
											<v-text-field required v-model="editMasterOrderItem.assemblydate"
												label="装配需求日" readonly v-bind="attrs" v-on="on"></v-text-field>
										</template>
										<v-date-picker locale="zh-cn" v-model="editMasterOrderItem.assemblydate"
											@input="assemblydateMenu = false"></v-date-picker>
									</v-menu>
								</v-col>
								<v-col cols="12" md="3">
									<v-text-field v-model="editMasterOrderItem.design_username" label="设计人员名称">
									</v-text-field>
								</v-col>
								<v-col cols="12" md="3">
									<v-text-field v-model="editMasterOrderItem.check_username" label="核对人员名称">
									</v-text-field>
								</v-col>
								<v-col cols="12" md="3">
									<v-text-field v-model="editMasterOrderItem.audit_username" label="审核人员名称">
									</v-text-field>
								</v-col>
								<v-col cols="12" md="3">
									<v-menu v-model="auditdateMenu" :close-on-content-click="false" :nudge-right="40"
										transition="scale-transition" offset-y min-width="auto">
										<template v-slot:activator="{ on, attrs }">
											<v-text-field required v-model="editMasterOrderItem.auditdate"
												label="最终审核完结日期" readonly v-bind="attrs" v-on="on"></v-text-field>
										</template>
										<v-date-picker locale="zh-cn" v-model="editMasterOrderItem.auditdate"
											@input="auditdateMenu = false"></v-date-picker>
									</v-menu>
								</v-col>
								<!-- 	<v-col cols="12" md="3">
									<v-text-field v-model="editMasterOrderItem.readyorbulk" label="整装/散装">
									</v-text-field>
								</v-col> -->
								<v-col cols="12">
									<v-textarea outlined name="input-7-4" label="备注"
										v-model="editMasterOrderItem.description"
										:value="editMasterOrderItem.description"></v-textarea>
								</v-col>
							</v-row>
							<!-- <v-spacer></v-spacer> -->
						</v-form>
					</v-col>
					<!-- <v-divider vertical></v-divider> -->
					<!-- <v-col cols="6">
						<v-alert text color="blue-grey">源数据</v-alert>
						<v-row>
							<v-col cols="12" md="3">
								<v-text-field disabled v-model="editMasterOrderItem.workcode" label="工单号">
								</v-text-field>
							</v-col>
							<v-col cols="12" md="3">
								<v-text-field disabled v-model="editMasterOrderItem.projectcode" label="项目号">
								</v-text-field>
							</v-col>
							<v-col cols="12" md="3">
								<v-text-field disabled v-model="editMasterOrderItem.typename" label="工单类型">
								</v-text-field>
							</v-col>
							<v-col cols="12" md="3">
								<v-text-field disabled v-model="editMasterOrderItem.customername" label="客户名称">
								</v-text-field>
							</v-col>
							<v-col cols="12" md="3">
								<v-text-field disabled v-model="editMasterOrderItem.qty" label="数量">
								</v-text-field>
							</v-col>
							<v-col cols="12" md="3">
								<v-text-field disabled v-model="editMasterOrderItem.unitname" label="单位名称">
								</v-text-field>
							</v-col>

							<v-col cols="12" md="3">
								<v-text-field disabled v-model="editMasterOrderItem.machinecode" label="机台号">
								</v-text-field>
							</v-col>
							<v-col cols="12" md="3">
								<v-text-field disabled v-model="editMasterOrderItem.versioncode" label="版本号">
								</v-text-field>
							</v-col>
							<v-col cols="12" md="3">
								<v-text-field disabled v-model="editMasterOrderItem.issueddate" label="下发日期">
								</v-text-field>
							</v-col>
							<v-col cols="12" md="3">
								<v-text-field disabled v-model="editMasterOrderItem.assemblydate" label="装配需求日">
								</v-text-field>
							</v-col>
							<v-col cols="12" md="3">
								<v-text-field disabled v-model="editMasterOrderItem.design_username" label="设计人员名称">
								</v-text-field>
							</v-col>
							<v-col cols="12" md="3">
								<v-text-field disabled v-model="editMasterOrderItem.check_username" label="核对人员名称">
								</v-text-field>
							</v-col>
							<v-col cols="12" md="3">
								<v-text-field disabled v-model="editMasterOrderItem.audit_username" label="审核人员名称">
								</v-text-field>
							</v-col>
							<v-col cols="12" md="3">
								<v-text-field disabled v-model="editMasterOrderItem.auditdate" label="最终审核完结日期">
								</v-text-field>
							</v-col>
							<v-col cols="12">
								<v-textarea disabled outlined name="input-7-4" label="备注"
									:value="editMasterOrderItem.description"></v-textarea>
							</v-col>
						</v-row>
					</v-col>
				 -->
				</v-row>
				<v-card-actions>
					<v-spacer></v-spacer>
					<v-btn class="mr-4" tile color="success" @click="submit">
						提交
					</v-btn>
					<v-btn color="secondary" tile dark @click="clear">
						取消更新
					</v-btn>
				</v-card-actions>
				<!-- <v-divider></v-divider> -->
				</v-list>
			</v-card>
		</v-dialog>
		<!-- 返修单对话框 -->
		<v-dialog v-if="backRepairMasterOrderItem!=null" v-model="backRepairDialogVisable" persistent
			max-width="1000px">
			<v-card>
				<v-card-title>
					<span class="text-h5">返修工单维护</span>
				</v-card-title>
				<v-card-text>
					<v-container>

						<v-expansion-panels>
							<v-expansion-panel>
								<v-expansion-panel-header>
									历史返修记录
								</v-expansion-panel-header>
								<v-expansion-panel-content>
									<v-simple-table>
										<template v-slot:default>
											<thead>
												<tr>
													<th class="text-center">
														返修开始时间
													</th>
													<th class="text-center">
														返修结束时间
													</th>
													<th class="text-center">
														返修最终交付时间
													</th>
													<th class="text-center">
														是否完结
													</th>
													<th class="text-center">
														备注
													</th>
													<th class="text-center">
														操作
													</th>
												</tr>
											</thead>
											<tbody>
												<tr v-for="m in backRepairHistoryItems" :key="m.id">
													<td class="text-center">{{m.starttime}}</td>
													<td class="text-center">{{m.endtime}}</td>
													<td class="text-center">{{m.deliverytime}}</td>
													<td class="text-center" v-if="m.status==2" style="color: #40e040;">
														{{m.statusname}}
													</td>
													<td class="text-center" v-else>{{m.statusname}}</td>
													<td class="text-center">{{m.description}}</td>
													<td class="text-center" v-if="m.status!=2">
														<v-btn small tile color="success"
															@click="editEveryBackRepairItem(m)">
															编辑返修单
														</v-btn>
													</td>
													<td class="text-center" v-else>
														<v-btn small tile depressed disabled>
															编辑返修单
														</v-btn>
													</td>
												</tr>
											</tbody>
										</template>
									</v-simple-table>
								</v-expansion-panel-content>
							</v-expansion-panel>
						</v-expansion-panels>
						<br />
						<v-divider></v-divider>
						<br />
						<v-row>
							<v-col cols="12" sm="6" md="4">
								<v-text-field label="项目号" disabled v-model="backRepairMasterOrderItem.projectcode">
								</v-text-field>
							</v-col>
							<v-col cols="12" sm="6" md="4">
								<v-text-field label="项目号名称" disabled v-model="backRepairMasterOrderItem.projectname">
								</v-text-field>
							</v-col>
							<v-col cols="12" sm="6" md="4">
								<v-text-field label="母工单号" disabled v-model="backRepairMasterOrderItem.workcode">
								</v-text-field>
							</v-col>
							<v-col cols="12" sm="6" md="4">
								<v-text-field label="母工单类型" disabled v-model="backRepairMasterOrderItem.typename">
								</v-text-field>
							</v-col>
							<v-col cols="12" sm="6" md="4">
								<v-select v-if="BackRepairDialogAddOrEdit.value==1" disabled
									v-model="backRepairItem.status=1" :items="backRepairStatusList" label="返修单状态">

								</v-select>
								<v-select v-else v-model="backRepairItem.status" :items="backRepairStatusList"
									label="返修单状态">
								</v-select>
							</v-col>
							<!--  -->
							<v-col cols="12" sm="6" md="4">
								<v-menu v-model="backRepairDeliverytimeMenu" :close-on-content-click="false"
									:nudge-right="40" transition="scale-transition" offset-y min-width="auto">
									<template v-slot:activator="{ on, attrs }">
										<v-text-field v-model="backRepairItem.deliverytime" label="返修最终交付时间" readonly
											v-bind="attrs" v-on="on" required></v-text-field>
									</template>
									<v-date-picker locale="zh-cn" v-model="backRepairItem.deliverytime"
										@input="backRepairDeliverytimeMenu = false">
									</v-date-picker>
								</v-menu>
							</v-col>
							<v-col cols="12">
								<v-textarea outlined name="input-7-4" label="备注" v-model="backRepairItem.description"
									:value="backRepairItem.description">
								</v-textarea>
							</v-col>
						</v-row>
					</v-container>
				</v-card-text>
				<v-card-actions>
					<v-spacer></v-spacer>
					<v-btn color="blue darken-1" text @click="editBackPairOrderSubmit">
						{{BackRepairDialogAddOrEdit.text}}
					</v-btn>
					<v-btn color="blue darken-1" text @click="editBackPairOrderClear">
						取消
					</v-btn>
				</v-card-actions>
			</v-card>
		</v-dialog>
		<!-- 子工单编辑对话框 -->
		<v-dialog v-model="childrenDialogVisable" fullscreen transition="dialog-bottom-transition">
			<v-card>
				<v-toolbar dark color="primary">
					<v-btn icon dark @click="childrenDialogVisable = false">
						<v-icon>mdi-close</v-icon>
					</v-btn>
					<v-toolbar-title>子工单信息变更</v-toolbar-title>
					<v-spacer></v-spacer>
				</v-toolbar>
				<v-row>

					<v-col cols="12">
						<v-alert text color="blue-grey" class="text-center">当前子工单信息</v-alert>
						<v-row>
							<v-col cols="12" sm="6" md="3">
								<v-text-field label="子工单号" disabled v-model="dialogCurrentEditChildrenOrderItem.code">
								</v-text-field>
							</v-col>
							<v-col cols="12" sm="6" md="3">
								<v-text-field label="图号" disabled v-model="dialogCurrentEditChildrenOrderItem.piccode">
								</v-text-field>
							</v-col>
							<v-col cols="12" sm="6" md="3">
								<v-text-field label="名称" disabled v-model="dialogCurrentEditChildrenOrderItem.name">
								</v-text-field>
							</v-col>
							<v-col cols="12" sm="6" md="3">
								<v-select disabled v-model="dialogCurrentEditChildrenOrderItem.type"
									:items="this.childrenTypeList" label="子工单类型"></v-select>
							</v-col>
							<v-col cols="6" md="3">
								<v-select v-model="dialogCurrentEditChildrenOrderItem.status"
									:items="this.childrenStatusList" label="工单状态"></v-select>
							</v-col>
							<v-col cols="12" sm="6" md="3">
								<v-text-field label="规格" v-model="dialogCurrentEditChildrenOrderItem.specifications">
								</v-text-field>
							</v-col>
							<v-col cols="12" sm="6" md="3">
								<v-text-field label="型号" v-model="dialogCurrentEditChildrenOrderItem.model">
								</v-text-field>
							</v-col>
							<v-col cols="12" sm="6" md="3">
								<v-text-field label="材料" v-model="dialogCurrentEditChildrenOrderItem.material">
								</v-text-field>
							</v-col>
							<v-col cols="12" sm="6" md="3">
								<v-text-field label="单套数量" v-model="dialogCurrentEditChildrenOrderItem.singlecount">
								</v-text-field>
							</v-col>
							<v-col cols="12" sm="6" md="3">
								<v-text-field label="总数" v-model="dialogCurrentEditChildrenOrderItem.totalcount">
								</v-text-field>
							</v-col>
							<v-col cols="12" sm="6" md="3">
								<v-text-field label="单位" v-model="dialogCurrentEditChildrenOrderItem.unit">
								</v-text-field>
							</v-col>
							<v-col cols="12" sm="6" md="3">
								<v-text-field label="表面处理"
									v-model="dialogCurrentEditChildrenOrderItem.surface_treatment"></v-text-field>
							</v-col>
							<!-- 		<v-col cols="12" sm="6" md="3">
									<v-text-field label="工艺" v-model="dialogCurrentEditChildrenOrderItem.process">
									</v-text-field>
								</v-col> -->
							<v-col cols="12" sm="6" md="3">
								<v-select v-model="dialogCurrentEditChildrenOrderItem.outinstoragelink"
									:items="this.childrenOutInStorageLinkList" label="出入库环节"></v-select>
							</v-col>
							<v-col cols="12" sm="6" md="3">
								<v-menu v-model="dialogCurrentEditChildrenOrderItemDeliverydateMenu"
									:close-on-content-click="false" :nudge-right="40" transition="scale-transition"
									offset-y min-width="auto">
									<template v-slot:activator="{ on, attrs }">
										<v-text-field v-model="dialogCurrentEditChildrenOrderItem.deliverydate"
											label="交期" readonly v-bind="attrs" v-on="on"></v-text-field>
									</template>
									<v-date-picker locale="zh-cn"
										v-model="dialogCurrentEditChildrenOrderItem.deliverydate"
										@input="dialogCurrentEditChildrenOrderItemDeliverydateMenu = false">
									</v-date-picker>
								</v-menu>
							</v-col>
							<v-col cols="12" sm="6" md="3">
								<v-menu v-model="dialogCurrentEditChildrenOrderItemConfirmdateMenu"
									:close-on-content-click="false" :nudge-right="40" transition="scale-transition"
									offset-y min-width="auto">
									<template v-slot:activator="{ on, attrs }">
										<v-text-field v-model="dialogCurrentEditChildrenOrderItem.confirmdate"
											label="确认日期" readonly v-bind="attrs" v-on="on"></v-text-field>
									</template>
									<v-date-picker locale="zh-cn"
										v-model="dialogCurrentEditChildrenOrderItem.confirmdate"
										@input="dialogCurrentEditChildrenOrderItemConfirmdateMenu = false">
									</v-date-picker>
								</v-menu>
							</v-col>
							<v-col cols="12">
								<v-textarea outlined name="input-7-4" label="备注"
									v-model="dialogCurrentEditChildrenOrderItem.description"
									:value="dialogCurrentEditChildrenOrderItem.description"></v-textarea>
							</v-col>
						</v-row>
					</v-col>
					<!-- <v-col cols="6">
						<v-alert text color="blue-grey">源信息</v-alert>
						<v-row>
							<v-col cols="12">
								<v-scroll-y-transition mode="out-in">
									<v-card class="pt-6 mx-auto" flat max-width="600">
										<v-card-text>

											<h3 class="headline mb-2">
												{{dialogCurrentEditChildrenOrderItem.name}}
											</h3>
											<div class="blue--text mb-2">

											</div>
											<div class="blue--text subheading font-weight-bold">
												开发者：
											</div>
										</v-card-text>
										<v-divider></v-divider>
										<v-row class="text-left" tag="v-card-text">
											<v-col class="text-right mr-4 mb-2" tag="strong" cols="5">
												所属公司:
											</v-col>
											<v-col></v-col>
											<v-col class="text-right mr-4 mb-2" tag="strong" cols="5">
												Website:
											</v-col>
											<v-col class="text-right mr-4 mb-2" tag="strong" cols="5">
												平台:
											</v-col>
											<v-col></v-col>
										</v-row>
									</v-card>
								</v-scroll-y-transition>
							</v-col>
						</v-row>
					</v-col>
				 -->
				</v-row>
				<v-card-actions>
					<v-spacer></v-spacer>
					<v-btn class="mr-4" tile color="success" @click="editChildrenSubmit">
						提交
					</v-btn>
					<v-btn color="secondary" tile dark @click="editChildrenClear">
						取消更新
					</v-btn>
				</v-card-actions>
			</v-card>
		</v-dialog>
		<!-- 返修对话框 -->
		<v-dialog v-if="dialogMmachineChangeMasterItem!=null" v-model="machineChangeMasterDialogVisable"
			max-width="800px">
			<v-card>
				<v-card-title>
					<span class="text-h5">设变工单维护</span>
				</v-card-title>
				<v-card-text>
					<v-row>
						<v-col cols="12">
							<v-textarea outlined label="设变信息描述" v-model="dialogMmachineChangeMasterItem.description"
								:value="dialogMmachineChangeMasterItem.description">
							</v-textarea>
						</v-col>
					</v-row>
				</v-card-text>
				<v-card-actions>
					<v-spacer></v-spacer>
					<v-btn color="blue darken-1" text @click="machineChangeMasterDialogSubmit">
						提交
					</v-btn>
					<v-btn color="blue darken-1" text @click="machineChangeMasterDialogClosse">
						取消
					</v-btn>
				</v-card-actions>
			</v-card>
		</v-dialog>
		<!-- 查看工艺对话框 -->
		<v-dialog v-model="gyDialogVisable" max-width="800px">
			<v-card class="mx-auto" outlined>
				<v-card-title>
					工艺信息
				</v-card-title>
				<v-card-text>
					<v-data-table height="450" small fixed-header dense :headers="leftGYHeaderList"
						:items="leftGYDataList" item-key="id" class="text-no-wrap" :footer-props="vuetifyDefaultPage">
					</v-data-table>
				</v-card-text>
				<v-card-actions>
					<v-spacer></v-spacer>
					<v-btn color="blue darken-1" text @click="gyDialogVisable=false">
						取消
					</v-btn>
				</v-card-actions>
			</v-card>
		</v-dialog>
	</div>

</template>

<script>
	export default {
		name: 'mes_production_workorder_manager',
		data() {
			return {
				vuetifyDefaultPage: {
					'items-per-page-text': '每页个数',
					'items-per-page-options': [10, 20, 50, 100]
					// 'items-per-page-all-text': '100'
				},
				query: {
					'projectname': '',
					'workcode': '',
					'name': ''
				},

				//工艺表头
				leftGYHeaderList: [{
						text: '执行顺序',
						value: 'order'
					},
					{
						text: '工艺代码',
						value: 'code'
					}, {
						text: '工艺名称',
						value: 'name'
					}, {
						text: '描述',
						value: 'description'
					}
				],
				//工艺数据
				leftGYDataList: [],
				//工艺对话框
				gyDialogVisable: false,
				//设变工单对话框
				machineChangeMasterDialogVisable: false,
				backRepairDialogVisable: false,
				//主工单编辑对话框
				editDialogVisable: false,
				// 母工单状态变更对话框
				masterStatusDialogVisable: false,
				searchInput: '',
				selectedInputType: '',
				inputTypeList: ['项目名称', '工单号', '设备名称'],
				//母工单列表
				singleExpand: false,
				masterOrderHeaderList: [{
						text: '操作',
						align: 'center',
						value: 'actions',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '项目号',
						align: 'center',
						value: 'projectcode',
						filterable: false
					},
					{
						text: '项目名称',
						align: 'center',
						value: 'projectname',
						filterable: false
					},
					{
						text: '工单号',
						value: 'workcode',
						filterable: false
					},
					{
						text: '设备名称',
						value: 'name',
						filterable: false
					},
					{
						text: '工单类型',
						value: 'typename',
						filterable: false
					},
					{
						text: '工单状态',
						value: 'statusname',
						filterable: false
					},
					{
						text: '数量',
						value: 'qty',
						filterable: false
					},
					{
						text: '单位',
						value: 'unitname',
						filterable: false
					},
					{
						text: '版本号',
						value: 'versioncode',
						filterable: false
					},
					{
						text: '下发日期',
						value: 'issueddate',
						filterable: false
					},
					{
						text: '需求日',
						value: 'assemblydate',
						filterable: false
					},
					{
						text: '设计',
						value: 'design_username',
						filterable: false
					},
					{
						text: '核对',
						value: 'check_username',
						filterable: false
					},
					{
						text: '审核',
						value: 'audit_username',
						filterable: false
					},
					{
						text: '备注',
						value: 'description',
						filterable: false
					},

				],
				masterOrderList: [],
				editMasterOrderItem: null,
				//返修工单时使用的主工单信息
				backRepairMasterOrderItem: null,
				//返修编辑后发送到后台的模型
				backRepairItem: {},
				BackRepairDialogAddOrEdit: {},
				//所有的返修记录
				backRepairHistoryItems: [],

				backRepairDeliverytimeMenu: false,
				//下发日期日期组件对话框
				issueddateMenu: false,
				//装配需求日期组件对话框
				assemblydateMenu: false,
				//最终审核完结日期对话框
				auditdateMenu: false,
				selectCurrentMasterOrderItem: {},
				childrenHeader: [],
				// 母工单的状态列表
				masterOrderStatusList: [],
				childrenStatusList: [],
				childrenTypeList: [],
				childrenOutInStorageLinkList: [],
				backRepairStatusList: [],
				allEnums: ['WorkOrderChildrenType', 'OutInStorageLink', 'WorkOrderStatus', 'WorkOrderChildrenStatus',
					'WorkOrderChildrenType', 'BackRepairStatus'
				],

				//主工单的状态是否变为：强制完结
				// masterStatusSaveFlag:false,
				childrenOrderList: [],


				childrenDialogVisable: false,

				dialogCurrentEditChildrenOrderItem: {},
				//交期
				dialogCurrentEditChildrenOrderItemDeliverydateMenu: false,
				//确认日期
				dialogCurrentEditChildrenOrderItemConfirmdateMenu: false,
				//设变工单信息
				dialogMmachineChangeMasterItem: null,
				ResourceWhereParamters: {
					WhereParamters: [],
				},
				masterOrderLoading: true,
				masterOrderTotalCount: 0,
				masterOrderOptions: {
					//页数
					page: 1,
					//每页行数
					itemsPerPage: 10
				},
				e6: 1,
			}
		},
		watch: {
			masterOrderOptions: {
				handler() {
					this.loadMasterOrderList()
				},
				deep: true
			}
		},
		created() {
			this.loadStatusTypeList()
		},
		methods: {
			async loadStatusTypeList() {
				var response = await this.$httpRequest({
					url: `MES/MESCommon/GetTypes`,
					method: 'post',
					data: this.allEnums
				})

				if (response.status == 1) {
					this.childrenTypeList = response.data.filter(r => r.enumName ==
						'WorkOrderChildrenType') // areaEnumsValue
					this.childrenOutInStorageLinkList = response.data.filter(r => r.enumName == 'OutInStorageLink')
					this.masterOrderStatusList = response.data.filter(r => r.enumName == 'WorkOrderStatus')
					this.childrenStatusList = response.data.filter(r => r.enumName == 'WorkOrderChildrenStatus')
					this.backRepairStatusList = response.data.filter(r => r.enumName == 'BackRepairStatus')
				}

			},
			searchMasterOrders() {
				this.masterOrderLoading = true
				var that = this
				this.$httpRequest({
					url: `MES/MESManagerOrder/LoadMasterOrderList?
					projectname=${this.query.projectname}&
					workcode=${this.query.workcode}&
					name=${this.query.name}&
					pageNumber=${this.masterOrderOptions.page}&
					pageSize=${this.masterOrderOptions.itemsPerPage}
					`,
					method: 'get',
				}).then(function(response) {
					if (response.status == 1) {
						that.masterOrderList = response.data.model.result
						that.masterOrderTotalCount = response.data.model.pagination.totalRecords
						that.masterOrderLoading = false
					} else {
						that.$message.error('加载主工单信息失败')
					}
				})
			},
			//加载工单信息
			loadMasterOrderList() {
				this.searchMasterOrders()
			},
			masterOrderExpandEvent(expand) {
				this.expandedChildrenList = expand

			},
			getMasterOrderStatusName(item) {

				console.log()
				var status = this.masterOrderStatusList.find(function(e) {
					return e.value == item.status
				})
				return status == null ? null : status.text
			},
			//更新主工单状态
			saveMasterStatus(item) {
				var that = this
				this.$httpRequest({
					url: `MES/MESManagerOrder/PartiallyUpdateMasterOrder/${item.id}`,
					method: 'patch',
					data: [{
						"operationType": 0,
						"path": "status",
						"op": "replace",
						"from": "string",
						"value": item.status
					}]
				}).then(function(response) {
					if (response.status == 1) {
						that.$message.success('状态更新成功')
						that.masterOrderList.forEach((r) => {
							if (r.id == item.id) {
								item.statusname = that.masterOrderStatusList.find(function(e) {
									return e.value == item.status
								}).value
								item.machineChange = response.data.machineChange
								r = item

							}
						})
					} else {
						that.$message.error(response.info)
					}
				})
			},
			//取消状态变更
			cancelSaveMasterStatus(prop) {
				console.log(prop)
			},
			//打开主工单编辑对话框
			openEditMasterDialog(masterItem) {
				this.editDialogVisable = true
				this.editMasterOrderItem = masterItem
			},
			//删除主工单
			deleteMasterItem(masterItem) {
				// this.$store.dispatch('snackbar/openSnackbar', {
				// 	msg: '瓦拉伊利五路',
				// 	color: 'primary'
				// })
				var that = this
				this.$httpRequest({
					url: `MES/MESManagerOrder/DeleteMasterOrder/${masterItem.id}`,
					method: 'delete'
				}).then(function(response) {
					if (response.status == 1) {
						that.$message.success('删除母工单成功')
						that.masterOrderList.splice(that.masterOrderList.findIndex(item => item.id == masterItem
							.id), 1)
						// objs.splice(objs.findIndex(item=>item.id==2),1)
					} else {
						that.$message.error('删除母工单失败')
					}
				})
			},
			//验证主工单Form表单
			validMasterOrderForm() {},
			//提交masterOrder Form表单
			submit() {
				var that = this
				this.$httpRequest({
					url: `MES/MESManagerOrder/UpdateMasterOrder`,
					method: 'put',
					data: this.editMasterOrderItem
				}).then(function(response) {
					if (response.status == 1) {
						that.$message.success('更新主工单成功')
						that.loadMasterOrderList()
						that.editDialogVisable = false
					} else {
						that.$message.error('更新主工单失败')
					}
				})
			},
			//取消表单
			clear() {
				this.editDialogVisable = false
			},
			//主工单展开事件
			async expandedMasterOrderItemEvent(expand) {
				this.childrenOrderList = []
				if (expand.value) {
					var item = expand.item

					var response = await this.$httpRequest({
						url: `MES/MESManagerOrder/LoadChildrenOrderList?
						masterid=${item.id}&
						pageNumber=1&
						pageSize=1000`,
						method: 'get'
					})
					if (response.status == 1) {
						this.childrenHeader = response.data.headers
						this.childrenOrderList = response.data.childrenList
					} else {
						this.$message.error('加载子工单失败')
					}
				} else {
					this.selectCurrentMasterOrderItem = {}
				}
			},
			//打开编辑子工单对话框
			openEditChildrenOrderDialog(item) {
				this.childrenDialogVisable = true
				this.dialogCurrentEditChildrenOrderItem = item
			},
			//删除子工单
			deleteChildrenOrderItem(item) {
				var that = this
				this.$httpRequest({
					url: `MES/MESManagerOrder/DeleteChildrenOrder/${item.id}`,
					method: 'delete'
				}).then(function(response) {
					if (response.status == 1) {
						that.childrenOrderList.splice(that.childrenOrderList.findIndex(r => r.id == item.id), 1)
					} else {
						console.log('删除子工单失败')
					}
				})
			},
			//提交子工单编辑
			editChildrenSubmit() {
				var that = this
				this.$httpRequest({
					url: `MES/MESManagerOrder/UpdateChildrenOrder`,
					method: 'put',
					data: this.dialogCurrentEditChildrenOrderItem
				}).then(function(response) {
					if (response.status == 1) {
						that.childrenDialogVisable = false
						that.$message.success('修改子工单成功')
						that.loadMasterOrderList()
					} else {
						that.$message.error('修改子工单失败')
					}
				})
			},
			//取消子工单编辑
			editChildrenClear() {
				this.childrenDialogVisable = false
			},
			//打开返修编辑对话框
			openBackrepairMasterDialog(masterItem) {
				this.backRepairDialogVisable = true
				this.backRepairMasterOrderItem = masterItem
				this.backRepairItem = {}
				this.BackRepairDialogAddOrEdit = {
					text: '新增返修单',
					value: '1'
				}
				var that = this
				this.$httpRequest({
					url: `MES/MESManagerOrder/GetBackRepairList/${this.backRepairMasterOrderItem.id}`,
					method: 'get'
				}).then(function(response) {
					if (response.status == 1) {
						that.backRepairHistoryItems = response.data.model.result
						// that.backRepairStatusList = response.data.other1
					} else {
						that.$message.error('获取返修工单失败')
					}
				})
			},
			//编辑返修列表的项目
			editEveryBackRepairItem(item) {
				this.backRepairItem = item
				this.BackRepairDialogAddOrEdit = {
					text: '修改',
					value: 2
				}
			},
			//提交返修单
			editBackPairOrderSubmit() {
				this.backRepairItem.masterid = this.backRepairMasterOrderItem.id
				var that = this
				var url;
				var method;
				if (this.BackRepairDialogAddOrEdit.value == 1) { //新增
					url = `MES/MESManagerOrder/AddBackRepair`
					method = 'post'
				} else {
					//修改
					url = `MES/MESManagerOrder/UpdateBackRepair`
					method = 'put'
				}
				this.$httpRequest({
					url: url,
					method: method,
					data: this.backRepairItem
				}).then(function(response) {
					if (response.status == 1) {
						that.backRepairDialogVisable = false
						that.backRepairItem = {}
						that.$message.success('编辑返修单成功')
					} else {
						that.$message.error('修改子工单失败')
					}
				})
			},
			//取消返修单
			editBackPairOrderClear() {
				this.backRepairDialogVisable = false
			},
			//打开设变工单对话框
			openMachineChangeMasterDialog(item) {
				this.machineChangeMasterDialogVisable = true
				this.dialogMmachineChangeMasterItem = item.machineChange
				if (this.dialogMmachineChangeMasterItem == null) {
					console.log('更新工单状态后需确认才可编辑操作')
				}
			},
			//关闭设变工单
			machineChangeMasterDialogClosse() {
				this.machineChangeMasterDialogVisable = false
			},
			//提交设变工单
			machineChangeMasterDialogSubmit() {
				var that = this
				this.$httpRequest({
					url: `MES/MESManagerOrder/UpdateMachineChange`,
					method: 'put',
					data: this.dialogMmachineChangeMasterItem
				}).then(function(response) {
					if (response.status == 1) {
						that.masterOrderList.find(function(e) {
							if (e.id == that.dialogMmachineChangeMasterItem.masterid) {
								e.machineChange = that.dialogMmachineChangeMasterItem
								return e
							}
						})
						that.machineChangeMasterDialogVisable = false
						that.$message.success('编辑设变工单成功')
					} else {
						console.log('')
						that.$message.error('修改设变工单失败')
					}
				})
			},
			async closeOrderClick(master) {
				var response = await this.$httpRequest({
					url: `MES/MESManagerOrder/Close?masterId=${master.id}`,
					method: 'get'
				})
				if (response.status == 1) {
					this.$message.success(response.info)
					this.searchMasterOrders()
				} else {
					this.$message.error(response.info)
				}
			},
			//查看工艺信息
			lookGYInfo(item) {
				this.leftGYDataList = item.processCardGYList
				this.gyDialogVisable = true
			}

		}
	}
</script>

<style>
	.splittext {
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.main {
		background: #e5ffd7;
	}
</style>
